package com.sun.decrypt.utils;

public class HexUtils {
    //把字节数组转16进制，如果一个字节转为16进制后不足两位，则前面补0
    public static String convertBytes2HexStr(byte[] digestBytes) {
        StringBuilder sb = new StringBuilder();
        for (byte digestByte : digestBytes) {
            String str = Integer.toHexString(((int) digestByte) & 0xff);
            if (str.length() == 1){
                str = "0" + str;
            }
            sb.append(str);
        }
        return sb.toString();
    }
    //把16进制字符串转字节数组
    public static byte[] converHexStrBytes(String hexStr){
        int length = hexStr.length() / 2;
        byte[] result = new byte[length];
        for (int i = 0; i < length; i++) {
            int high4 = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low4 = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high4*16 + low4);
        }
        return result;
    }
}
